//import 'package:amap_base/amap_base.dart';

import 'dart:async';
import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hotwork/HomePage.dart';
import 'package:flutter_hotwork/bean/messagebean.dart';
import 'package:flutter_hotwork/homepage/BaseTitleBar.dart';
import 'package:flutter_hotwork/homepage/BaseViewBar.dart';
import 'package:flutter_hotwork/utils/DioUtils.dart';
import 'package:flutter_hotwork/zhifa/QuestionListBean.dart';
import 'package:fluttertoast/fluttertoast.dart';
// import 'package:flutter_map/flutter_map.dart';
import 'package:latlong/latlong.dart';

import '../../Myapplication.dart';
import '../../Toast.dart';

class messagecenterPage extends StatefulWidget {
  @override
  messagecenterState createState() => new messagecenterState();
}
messagebean messagelist;
bool hasdata = false;
var _value;
class messagecenterState extends State<messagecenterPage> {

  @override
  void initState() {
    super.initState();
    DioUtils.createInstance();
  }

  @override
  void dispose() {
//    _amapLocation.stopLocate();
    super.dispose();
  }
  void getHotUserInfo() async {
    try {
      Response response = await DioUtils.dio.get(DioUtils.API_PREFIX+"/AlarmData/getHistoryAlarm?hotUserId="+MyApplication.loginuserinfo.userInfo.hotuserId+"&type=0&alarmStatus=0&alertSatus=0&pageNo=1&pageSize=100&startTime=2020-01-01 01:01:01");

      Map respMap = json.decode(response.data);
      messagelist  = messagebean.fromJson(respMap);
      if (messagelist.code == 0) {
        if(messagelist.result.records.length>0){
          hasdata = true;
          setState(() {
          });
        }
        // rebuild();
      } else {
        LocalToast.toast(context,msg: messagelist.message);
      }
      print('响应数据：' + response.toString());
    } catch (e) {
      //  LocalToast.toast(context,msg: "网络错误");
      print('请求出错：' + e.toString());
    }
  }


  void getHotUserInfo2() async {
    try {
      Response response = await DioUtils.dio.get(DioUtils.API_PREFIX+"/AlarmData/getHistoryAlarm?hotUserId="+MyApplication.loginuserinfo.userInfo.hotuserId+"&type=1&alarmStatus=0&alertSatus=0&pageNo=1&pageSize=100&startTime=2020-01-01 01:01:01");

    Map respMap = json.decode(response.data);
    messagelist  = messagebean.fromJson(respMap);
    if (messagelist.code == 0) {
    if(messagelist.result.records.length>0){
    hasdata = true;
    setState(() {
    });
    }



    // rebuild();
    } else {
    LocalToast.toast(context,msg: messagelist.message);
    }
    print('响应数据：' + response.toString());
    } catch (e) {
    //  LocalToast.toast(context,msg: "网络错误");
    print('请求出错：' + e.toString());
    }
  }
  TabController _tabController;

  @override
  Widget build(BuildContext context) {
    if(!hasdata){
      getHotUserInfo();
    }
    ScrollController controller = ScrollController();

    return Scaffold(
      appBar: new BaseViewBar(
          childView: new BaseTitleBar(
            "消息中心",
            leftIcon: 'images/fanhui.png',
            rightIcon:  '',
            rightClick: () {
            },
            leftClick: () {
              print("点击了左边干嘛啊。。。哦");
              Navigator.of(context).pop();
            },
          ),
          preferredSize: Size.fromHeight(50.0)),

      body:

      hasdata?
      Container(
        child:
        Column(
          children: [
            Row(children:[
              Container(width: 20,),
            DropdownButton(
                value: _value,

                hint: Text('请选择消息类型'),
                items: [
                  DropdownMenuItem(child: Text('采集报警'),value: 1,),
                  DropdownMenuItem(child: Text('系统报警'),value: 2,)
                ],onChanged: (value) {
                  if(value==1){
                    getHotUserInfo();
                  }else{
                    getHotUserInfo2();
                  }
                  _value = value;

            })
              ,]),

            Expanded(
              child:
              genContentList(
                  messagelist.result.records,context, controller
              ),
            ),
          ],
        ),
      ):
      Container(

        child:
        Column(
          mainAxisAlignment:MainAxisAlignment.center,
          crossAxisAlignment:CrossAxisAlignment.center,
          children: [
            Row(
              mainAxisAlignment:MainAxisAlignment.center,
              crossAxisAlignment:CrossAxisAlignment.center,
              children: [
                Text("暂无消息"),
              ],

            ),


          ],
        )
        ,
      ),
    );
  }


  genContentList(List<Records> data, BuildContext context, controller){
    double rpx = MediaQuery.of(context).size.width / 750;
    return ListView.builder(
        shrinkWrap: true,
        controller: controller,
        itemCount: data.length,
        itemBuilder:(BuildContext context, int index){
          return Container(

            decoration: BoxDecoration(
              color: Colors.white,
              boxShadow: <BoxShadow>[//设置阴影
                new BoxShadow(
                  color: Colors.grey,//阴影颜色
                  blurRadius:1.0,//阴影大小
                ),
              ],
            ),

            margin: EdgeInsets.only(left: 10,top: 0,right: 10,bottom:10),
            // height: 260 * rpx,
            child: Column(
              children: <Widget>[
                Container(
                  height: 10,
                ),
                Container(
                  padding: EdgeInsets.only(left: 10,top: 0,right: 10,bottom:0),
                  child: Row(
                    crossAxisAlignment:CrossAxisAlignment.center,
                    children: [
                      Container(
                        width: 10,
                      ),
                      Text(data[index].lineName+"/"+data[index].companyName,style: new TextStyle(color: Color(0xFF333333),
                        fontSize: 14.0,
                      ),
                        maxLines: 3,
                        softWrap: true,
                      ),
                      Flexible(fit: FlexFit.tight, child: SizedBox()),

                    ],
                  ),
                ),
                Container(
                  padding: EdgeInsets.only(left: 10,top: 0,right: 10,bottom:0),
                  margin: EdgeInsets.only(left: 0,top: 10,right: 0,bottom: 10),
                  child: Row(
                    children: [
                      // Image(height:20,width: 20,image: AssetImage('images/mingcheng.png')),
                      Container(
                        width: 10,
                      ),
                      Expanded(
                        child:
                        Text(data[index].alertDescribe,style: new TextStyle(color: Color(0xFF666666),
                          fontSize: 12.0,
                        ),
                          maxLines: 10,
                          softWrap: true,
                        ),
                      ),

                    ],
                  ),
                ),


                Container(
                  padding: EdgeInsets.only(left: 10,top: 0,right: 10,bottom:10),
                  child: Row(
                    crossAxisAlignment:CrossAxisAlignment.center,
                    children: [
                      Container(
                        width: 10,
                      ),
                      Text(data[index].alertStartTime,style: new TextStyle(color: Color(0xFF666666),
                        fontSize: 12.0,
                      ),
                        maxLines: 3,
                        softWrap: true,),
                      Flexible(fit: FlexFit.tight, child: SizedBox()),

                    ],
                  ),
                ),
              ],
            ),
          );
        }
    );
  }

}






















